package com.hangzhou.welbeing.welbeinginstrument.fragement;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.activity.SimilarProfilePageFocusedActivity;
import com.hangzhou.welbeing.welbeinginstrument.adapter.NewPostAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.EventBussFlag;
import com.hangzhou.welbeing.welbeinginstrument.bean.NewPostBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 08.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.fragement
 * 工程名 welbeing_pregnant
 *
 * @desc 最新帖子
 */
public class NewPostFragment extends Fragment {
    private static final String TAG = "NewPostFragment";
    @Bind(R.id.fragment_new_post_recyclerView)
    RecyclerView fragmentNewPostRecyclerView;

    private Context mcontent = MyApplication.getMcontext();
    private NewPostAdapter newPostAdapter;
    private List<NewPostBean.DataBean> mlist;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(container.getContext(), R.layout.fragment_new_post, null);
        ButterKnife.bind(this, view);
        // 1. 注册
        EventBus.getDefault().register(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentNewPostRecyclerView.setLayoutManager(layoutManager);
        newPostAdapter = new NewPostAdapter(mcontent);
        fragmentNewPostRecyclerView.setAdapter(newPostAdapter);
        newPostAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
               /* Intent intent = new Intent(getContext(), PersonalArticlesActivity.class);
                startActivity(intent);*/
                int position = fragmentNewPostRecyclerView.getChildAdapterPosition(view);
                Intent intent = new Intent(getContext(), SimilarProfilePageFocusedActivity.class);
                intent.putExtra("po_id",mlist.get(position).po_id);
               // intent.putExtra("po_url",mlist.get(position).po_url);
                intent.putExtra("po_name",mlist.get(position).po_name);
                intent.putExtra("content",mlist.get(position).content);
                intent.putExtra("cus_logo",mlist.get(position).cus_logo);
                intent.putExtra("addtime",mlist.get(position).addtime);
                intent.putExtra("cus_name",mlist.get(position).cus_name);
                intent.putExtra("state",mlist.get(position).state+"");
                intent.putExtra("count",mlist.get(position).count+"");
                intent.putExtra("cus_id",mlist.get(position).cus_id+"");
                intent.putExtra("col",mlist.get(position).col+"");
                intent.putExtra("lCount",mlist.get(position).lCount+"");
                intent.putExtra("lstate",mlist.get(position).lstate+"");
                intent.putExtra("hits_like",mlist.get(position).hits_like+"");
                startActivity(intent);

            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {

            }
        });
        loadNewPost();
        return view;
    }

    // 4. 接收消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBussFlag event) {
        Boolean flag =  event.isFlag();
        if(flag){
            loadNewPost();
        }

    }
    /**
     * @desc 加载最新帖子的接口 http://192.168.1.15/Public/Uploads/
     * */
    private void loadNewPost() {
        SharedPreferences sp = mcontent.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
       String circle_id =  sp.getString("circle_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.NEW_POST)
                .addParams("cat_id", circle_id)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final NewPostBean newPostBean = gson.fromJson(praiseParseNetworkResponse,NewPostBean.class);
                        if(newPostBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    mlist = newPostBean.data;
                                    if(mlist != null){
                                        newPostAdapter.addItem(mlist);
                                        newPostAdapter.notifyDataSetChanged();
                                    }
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(mcontent,newPostBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        // 2. 解注册
        EventBus.getDefault().unregister(this);
    }
}
